package com.rxyb.security.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rxyb.security.db.bean.SysUsercustom;
import com.rxyb.security.db.dao.SysCustomMapper;
import com.rxyb.security.db.dao.SysMenuResouceMapper;
import com.rxyb.security.db.dao.SysRoleMapper;
import com.rxyb.security.db.dao.SysUserMapper;
import com.rxyb.security.db.po.SysRole;
import com.rxyb.security.db.po.SysUser;
import com.rxyb.security.db.po.custom.MenuRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author YJH
 * @version 1.0
 * @description 用户登录操作信息
 * @date 2020/7/7 11:27
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {


    private final SysUserMapper sysUserMapper;

    private final SysRoleMapper sysRoleMapper;

    private final SysMenuResouceMapper sysMenuResouceMapper;

    private final SysCustomMapper sysCustomMapper;

    public UserDetailsServiceImpl(SysUserMapper sysUserMapper, SysRoleMapper sysRoleMapper, SysMenuResouceMapper sysMenuResouceMapper, SysCustomMapper sysCustomMapper) {
        this.sysUserMapper = sysUserMapper;
        this.sysRoleMapper = sysRoleMapper;
        this.sysMenuResouceMapper = sysMenuResouceMapper;
        this.sysCustomMapper = sysCustomMapper;
    }

    /**
     * 根据用户名定位用户。在实际实现中，搜索
     * 可能是区分大小写的，或者不区分大小写，这取决于
     * 已配置实现实例。在本例中，<code>UserDetails</code>
     * 返回的对象可能有不同的用户名
     * 实际上已被请求..
     *
     * @param 标识需要其数据的用户的用户名。
     * @return a fully populated user record (never <code>null</code>)
     * @throws UsernameNotFoundException if the user could not be found or the user has no
     *                                   GrantedAuthority
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (null == username || username.trim().length() <= 0) {
            throw new UsernameNotFoundException("用户名为空");
        }
        // 查询用户信息
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.lambda().eq(SysUser::getUsername, username);
        SysUser sysUser = sysUserMapper.selectOne(sysUserQueryWrapper);

        // 获取角色信息
        List<MenuRole> roleId = sysCustomMapper.findRoleId(sysUser.getRoleId());
        log.info("roleId-->" + roleId.size());

        // 获取菜单按钮
        List<MenuRole> menuId = sysCustomMapper.findMenuId(roleId.stream().map(MenuRole::getId).collect(Collectors.toList()));

        SysUsercustom usercustom = new SysUsercustom();
        usercustom.setUsername(sysUser.getUsername());
        usercustom.setPassword(sysUser.getPassword());
        usercustom.setSysRoleList(roleId.stream().sequential().collect(Collectors.toCollection(() -> menuId)));
        return usercustom;
    }
}
